<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="名称">
        <el-input size="small" v-model="listQuery.q_name" minlength="2" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
        <el-button icon="el-icon-refresh-left" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="append" v-permission="['admin/system/newsParter/create']">添加
      </el-button>
      <el-button size="small" type="primary" @click="edit" v-permission="['admin/system/newsParter/update']">修改
      </el-button>
      <el-button size="small" type="primary" @click="deleteout" v-permission="['admin/system/newsParter/del']">删除
      </el-button>
    </div>
    <el-table ref="singleTable" :data="dataList" v-loading="listLoading" border fit highlight-current-row
      @current-change="handleCurrentChange" style="width: 100%">
      <el-table-column type="index" align="center" width="50">
      </el-table-column>
      <el-table-column property="name" width="200" label="名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="url" width="200" label="链接" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="sort" width="160" label="权重">
      </el-table-column>
      <el-table-column property="createTime" label="创建时间">
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <el-dialog :title="judgmentTitle" :visible.sync="dialogFormVisible" width="55%">
      <el-form :model="commissionForm" :rules="rules" ref="commissionForm">
        <el-form-item label="合作伙伴" prop="name" :label-width="formLabelWidth">
          <el-input v-model="commissionForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="链接地址" prop="url" :label-width="formLabelWidth">
          <el-input v-model="commissionForm.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort" :label-width="formLabelWidth">
          <el-input-number v-model="commissionForm.sort" :min="0" :max="99" label=""></el-input-number>
          <span class="form-word-hint">序号越小越靠前显示 最大99</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('commissionForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import Pagination from '@/components/Pagination'
  import {
    fetchNewsParterList,
    createNewsParter,
    toUpdateNewsParter,
    updateNewsParter,
    delNewsParter
  } from '@/api/news';
  import {
    Message
  } from "element-ui";
  export default {
    name: 'newsParterList',
    components: {
      Pagination
    },
    data() {
      return {
        dataList: [],
        total: 0,
        listLoading: false,
        listQuery: {
          q_name: '',
          page: 1,
          limit: 20
        },
        currentRow: null,
        judgmentTitle: '添加合作伙伴',
        judgmentType: '1',
        dialogFormVisible: false,
        formLabelWidth: '80px',
        commissionForm: {
          name: '',
          url: '',
          sort: ''
        },
        rules: {
          name: [{
              required: true,
              message: '请输入合作伙伴',
              trigger: 'blur'
            },
            {
              max: 20,
              message: '不能超过20个字符',
              trigger: 'blur'
            }
          ],
          url: [{
              required: true,
              message: '请输入链接地址',
              trigger: 'blur'
            },
            {
              max: 50,
              message: '不能超过50个字符',
              trigger: 'blur'
            }
          ],
          sort: [{
            required: true,
            message: '请输入排序号',
            trigger: 'blur'
          }, ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchNewsParterList(this.listQuery).then(res => {
          this.dataList = res.data.rows
          this.total = res.data.total
          this.listLoading = false
        })
      },
      handleCurrentChange(val) {
        // 选中当前行数据
        this.currentRow = val
      },
      append() {
        this.judgmentTitle = '添加合作伙伴'
        this.judgmentType = '1'
        this.commissionForm.name = ''
        this.commissionForm.url = ''
        this.commissionForm.sort = ''
        this.dialogFormVisible = true;
      },
      edit() {
        if (!this.currentRow) {
          Message.error("请选择操作行。");
          return;
        }
        this.judgmentTitle = '修改合作伙伴'
        this.judgmentType = '2'
        this.dialogFormVisible = true;
        toUpdateNewsParter({
          id: this.currentRow.id
        }).then(res => {
          this.commissionForm.name = res.data.data.newsPartner.name
          this.commissionForm.sort = res.data.data.newsPartner.sort
          this.commissionForm.url = res.data.data.newsPartner.url
        })
      },
      deleteout() {
        if (!this.currentRow) {
          Message.error("请选择操作行。");
          return;
        }
        this.$confirm("确定要删除吗？", "提示", {
          onfirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          delNewsParter({
            id: this.currentRow.id
          }).then(res => {
            if (res.data.success) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getList()
            } else {
              this.$message({
                type: "error",
                message: res.data.message
              });
            }
          })
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.judgmentType == '1') {
              createNewsParter(this.commissionForm).then(res => {
                if (!res.data.success) {
                  Message.error(res.data.message);
                  return;
                }
                Message.success('操作成功')
                this.dialogFormVisible = false;
                this.getList()
              })
            } else {
              this.commissionForm.id = this.currentRow.id
              updateNewsParter(this.commissionForm).then(res => {
                if (!res.data.success) {
                  Message.error(res.data.message);
                  return;
                }
                Message.success('操作成功')
                this.dialogFormVisible = false;
                this.getList()
              })
            }
          }
        });
      },
      reset() {
        this.listQuery.q_name = ''
        this.getList()
      }
    }
  }
</script>
<style scope>
  .text-annotation {
    border-style: solid;
    border-width: 1px 1px 1px 5px;
    background: 0 0;
    border-color: #e6e6e6;
    margin-bottom: 30px;
    padding: 15px;
    line-height: 22px;
    border-radius: 0 2px 2px 0;
    color: #666;
    font-size: 14px;
  }
</style>
